package com.xbh.management.utils;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xbh.common.utils.JwtUtil;
import com.xbh.management.model.ManDepartment;
import com.xbh.management.model.ManDeptUser;
import com.xbh.management.model.ManMenu;
import com.xbh.management.service.DepartmentService;
import com.xbh.management.service.ManDeptUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: management-center
 * @description:
 * @author: 许宝华
 * @create: 2022-03-23 11:04
 */

@Service
public class DepartmentUtil {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ManDeptUserService manDeptUserService;

    /**
     * 判断当前登录人是否为部门管理者，并且查出该部门下所有员工
     * @param treeNodeList
     * @param parentIds
     * @return
     */
    public List<Long> returnUserIdListByDepartmentLeader(){
        LambdaQueryWrapper<ManDepartment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ManDepartment::getDeleted,new Integer(0));
        List<ManDepartment> list = departmentService.list(wrapper);

        ArrayList<Long> userIdList = new ArrayList<>();
        list.stream().forEach( dept ->{
            if(ObjectUtil.isNotEmpty(dept.getUserId())){
                //当前登录人是这个部门领导
                if(dept.getUserId().equals(JwtUtil.getUserId())){
                    //查出该部门下的所有员工
                    LambdaQueryWrapper<ManDeptUser> manDeptUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    manDeptUserLambdaQueryWrapper.eq(ManDeptUser::getDepartmentId,dept.getDepartmentId());
                    List<ManDeptUser> deptUserList = manDeptUserService.list(manDeptUserLambdaQueryWrapper);
                    List<Long> userIdList1 = deptUserList.stream().map(depts -> {
                        return depts.getUserId();
                    }).collect(Collectors.toList());
                    userIdList.addAll(userIdList1);
                }
            }

        });
        return userIdList;

    }
}
